package com.roy.sms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.roy.sms.model.Column;
import com.roy.sms.model.User;
import com.roy.sms.service.ColumnService;
import com.roy.sms.service.UserService;

@Controller
public class LoginContrller extends BaseController{

	@Resource
	private UserService userService;
	@Resource
	private ColumnService columnService;
	
	@RequestMapping("/managerIndex.do")
	public String manager() {
		return "index";
	}
	
	@RequestMapping("/admin/subLogin.do")
	public String login(String username, String password, HttpServletRequest req){
		User user = userService.login(username, password);
		if(user == null){
			return REDIRECT + "/site/login.do";
		} else { 
			HttpSession session = req.getSession();
			session.setAttribute("loginUser", user);
			return REDIRECT + "/managerIndex.do";
		}
	}
	
	@RequestMapping("/admin/getUserColumn.do")
	@ResponseBody
	public List<Column> getUserColumns(HttpSession session){
		User user = (User) session.getAttribute("loginUser");
		if(user.getUsername().equals("admin")){
			return columnService.getColumns(-2);
		}
		return userService.getUserColumns(user);
	}
	
	@RequestMapping("/admin/logout.do")
	public String logout(HttpSession session){
		session.invalidate();
		return REDIRECT + "/index.jsp";
	}
	
}
